package org.PR.gui.windows;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.PR.gui.elements.GriddedPanel;

public class MainWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//private Logger logger = LoggerFactory.getLogger(MainWindow.class);
	
	private GriddedPanel panel = null;
	private JLabel xmlFileLabel = null;
	private JButton xmlFileButton = null;
	private final JFileChooser xmlFileChooser = new JFileChooser();

	public MainWindow(String title) {
		super(title);
		setParam();
	}
	
	protected void setParam()
	{
		int windowWidth = 400;           // Window width in pixels
	    int windowHeight = 150;          // Window height in pixels
	    this.setBounds(50, 100,       	 // Set position
	         windowWidth, windowHeight); // and size
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    
	    if(panel == null)
	    	panel = new GriddedPanel();	    
	    if(xmlFileLabel == null)
	    	xmlFileLabel = new JLabel("LABEL");
	    if(xmlFileButton == null)
	    {
	    	xmlFileButton = new JButton("OPEN FILE");
	    	xmlFileButton.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent arg0) {
					showFileChooserWindow();
				}
			});
	    }
	    panel.addComponent(xmlFileLabel, 1, 1);
	    panel.addComponent(xmlFileButton, 1, 2);
	    this.add(panel);
	}
	
	protected void showFileChooserWindow()
	{
		System.out.println("chooser");
		int result = xmlFileChooser.showOpenDialog(this);
		
		if (result == JFileChooser.APPROVE_OPTION) {
            File file = xmlFileChooser.getSelectedFile();
            //This is where a real application would open the file.
            System.out.println(file.getName());
            //logger.info("Opening: " + file.getName() + "." + "\n");
        } else {
            //logger.info("Open command cancelled by user." + "\n");
        }
	}
}
